package com.novanok.campaigntweets;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

/**
 * Created by Marcos on 12/10/2014.
 */
public class ActivityTweet extends Activity {

    private static final String TAG = "ActivityTweet";
    public static Tweet tweetSeleccionado = null;

    private Campania campaniaSeleccionada = null;

    @Override
    protected void onCreate( Bundle savedInstanceState ) {
        super.onCreate( savedInstanceState );
        setContentView(R.layout.activity_tweet);

        this.campaniaSeleccionada = MyActivity.campaniaSeleccionada;

        if ( this.campaniaSeleccionada != null ) {
            (( TextView ) findViewById( R.id.lblCampania )).setText( "Tweets de la Campaña ( " + this.campaniaSeleccionada.getNombre() + ")" );
            (( TextView ) findViewById( R.id.lblCampaniaDescripcion )).setText( this.campaniaSeleccionada.getDescripcion() );
        }

        setClickLista();
    }

    public void onResume(){
        super.onResume();
        this.campaniaSeleccionada = MyActivity.campaniaSeleccionada;

        if ( this.campaniaSeleccionada != null && this.campaniaSeleccionada.getTweets() != null ) {
            ((ListView) ActivityTweet.this.findViewById(R.id.lstTweets)).setAdapter(
                    new ListaTweetAdapter( ActivityTweet.this, this.campaniaSeleccionada.getTweets() ) );
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_tweets, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch ( item.getItemId() ) {
            case R.id.nuevo_tweet:
                this.nuevoTweet();
                return true;

            case R.id.editar_tweet:
                this.editarTweet();
                return true;

            case R.id.eliminar_tweet:
                this.eliminarTweet();
                return true;

            default:
                return super.onOptionsItemSelected(item);
        }
    }

    public void eliminarTweet (){
        ListaTweetAdapter adapter = ( ListaTweetAdapter )( ( ListView ) findViewById( R.id.lstTweets ) ).getAdapter();
        if ( tweetSeleccionado != null ) {
            this.campaniaSeleccionada.getTweets().remove( tweetSeleccionado );
            adapter.notifyDataSetChanged();
        }
    }

    public void nuevoTweet(){
        Intent i = new Intent(this, ActivityTweetItem.class);
        i.putExtra("tweet_accion", "nuevo");
        startActivity(i);
    }

    public void editarTweet(){
        Intent i = new Intent(this, ActivityTweetItem.class);
        i.putExtra("tweet_accion", "editar");
        startActivity(i);
    }

    private void setClickLista(){
        ListView lv = ( ListView )findViewById( R.id.lstTweets );

        lv.setOnItemClickListener( new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick( AdapterView<?> adapterView, View view, int i, long l ) {
                view.setSelected( true );

                ListaTweetAdapter adapter = ( ListaTweetAdapter )( ( ListView ) findViewById( R.id.lstTweets ) ).getAdapter();
                Tweet toRemove = adapter.getItem( ( ( ListView ) findViewById( R.id.lstTweets ) ).getPositionForView( view ) );
                ActivityTweet.tweetSeleccionado = toRemove;

                Log.d( TAG, "Click Item Lista " + toRemove.getDescripcion() );
            }
        });
    }
}
